const request = require('supertest')
const app = require('../app')
const should = require('chai').should()
const expect = require('chai').expect

const server = app.listen(9900)

const user = {
  username: 'root',
  password: '200050'
}

const user2 = {
  username: 'root',
  password: '20005'
}

const user1 = {
  username: 'sss',
  password: '12345'
}
const user3 = {
  username: 'sss'
}

const user4 = {
  username: 'JoJo',
  password: 'ljh200050',
  initialLevel: 3,
  planning: 4,
  learnStyle: 3,
  learnPurpose: 1
}
const user5 = {
  username: 'root',
  password: 'ljh200050',
  initialLevel: 3,
  planning: 4,
  learnStyle: 3,
  learnPurpose: 1
}
const user6 = {
  username: 'MiXi',
  password: 'ljh200050',
  initialLevel: 3,
  planning: 4,
  learnStyle: 3,
}

describe('外部测试', function () {
  it('登录', async function () {
    const res = await request(server)
      .post('/learningSpace/login')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user))
    expect(res.body.status).to.equal(200)
    expect(res.body.data).to.have.property('token')
  })
  it('密码错误', async function () {
    const res = await request(server)
      .post('/learningSpace/login')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user2))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('密码错误')
  })
  it('用户名错误', async function () {
    const res = await request(server)
      .post('/learningSpace/login')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user1))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('没有该用户')
  })

  it('登录参数缺失', async function () {
    const res = await request(server)
      .post('/learningSpace/login')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user3))
    expect(res.body).to.has.property('errors')
    expect(res.body.message).to.equal('Validation Failed')
  })

  it.skip('注册', async function () {
    const res = await request(server)
      .post('/learningSpace/register')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user4))
    expect(res.body.status).to.equal(200)
    expect(res.body.message).to.equal('用户创建成功')
    expect(res.body.data).to.have.property('token')
  })

  it('注册用户名重复', async function () {
    const res = await request(server)
      .post('/learningSpace/register')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user5))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('用户名已存在,请更换用户名重新注册')
  })

  it('注册参数缺失', async function () {
    const res = await request(server)
      .post('/learningSpace/register')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user6))
    expect(res.body).to.has.property('errors')
    expect(res.body.message).to.equal('Validation Failed')
  })
})
